package com.chiaching.chaoxingsecondkill.utils;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * @author jiajing.he
 * @date 3/26/21 6:18 PM
 */
@Component
@Slf4j
public class NotifyUtils {

    private static final String apiUrl="https://pushbear.ftqq.com/sub";

    private static String key="22143-5cb8de4645fdc108df04db5516c7508c";

    private static String testKey="22144-dc51bb2ee7dbabf93f8ac0d76637d67d";

    private static final String apiUrlBak=" https://sc.ftqq.com/SCU144751Tdf516f6e68a0c69f89fc6b7b72ad4f3c5ff2bebb9dd43.send";


    public static final String PRO="pro";

    public static final String TEST="test";

    public static void pushMsg(String pro,String title,String content){
        try {
            HashMap<String, Object> param = new HashMap<>(8);
            param.put("sendkey", PRO.equals(pro)?key:testKey);
            param.put("text", title);
            param.put("desp", content);
            String body = HttpUtil.createPost(apiUrl).form(param).execute().body();
            JSONObject jsonObject = new JSONObject(body);
            String res=jsonObject.getStr("code")+" "+jsonObject.getStr("data")+" "+URLDecoder.decode(jsonObject.getStr("message"));
            System.out.println(res);
            log.info(res);
        } catch (HttpException e) {
            log.error("发送微信通知失败!",e);
        }
    }

}
